package se.webbzon.oschi01.worldcamera;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Node;
import javax.media.j3d.ViewSpecificGroup;
import javax.swing.JPanel;

import se.webbzon.oschi01.worldarea.WorldAppearance3D;

import com.sun.j3d.exp.swing.JCanvas3D;

@Deprecated
public class JWorldView extends JPanel implements WorldView {
	
	// Auto generated serial version UID
	private static final long serialVersionUID = 2181791338606941141L;

	// The available properties of the JWorldView
	public static final int	TRANSPARENCY	= (1<<0);
	
	// The camera components of this world view
	private final WorldCamera camera;
	private final WorldCameraHud hud;
	private final JWorldCameraBackground background;
	
	// The Canvas3D associated with this JWorldView
	private final Canvas3D canvas3d;
	
	// The branch group of this view
	private final BranchGroup branchGroup;
	
	/*============================================================
	Constructors
	============================================================*/

	public JWorldView(int viewProperties,Dimension d) {
		setLayout(new BorderLayout());
		// Create the 3d canvas
		JCanvas3D jCanvas3d = new ExtendedJCanvas3D(d);
		setPreferredSize(d);
		add(jCanvas3d,BorderLayout.CENTER);
		canvas3d = jCanvas3d.getOffscreenCanvas3D();
		
		// Create the world camera components
		hud = new WorldCameraHud(canvas3d,d);
		background = new JWorldCameraBackground(canvas3d,d);
		/*if (isSet(viewProperties,TRANSPARENCY))
			camera = new WorldCamera(WorldCamera.TRANSPARENCY,canvas3d);
		else
			camera = new WorldCamera(0,canvas3d);*/
		camera = null;
		
		// Create the Java3D camera structure
		branchGroup = new BranchGroup(); 
		
		// Add a view specific group
		ViewSpecificGroup viewGroup = new ViewSpecificGroup();

		// Add background to view specific group
		/*viewGroup.addChild(background.getNode());
		viewGroup.addView(camera.getView());

		// Add sub groups to branch group
		branchGroup.addChild(viewGroup);
		branchGroup.addChild(camera.getNode());*/
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/*@Override public BranchGroup getJ3dBranch() {
		return branchGroup;
	}*/
	
	@Override public final WorldCamera getCamera() {
		return camera;
	}
	
	@Override public final WorldCameraPane getCameraHud() {
		return hud;
	}
	
	@Override public final WorldCameraPane getCameraBackground() {
		return background;
	}
	
	/** Returns the Canvas3d instance associated with this JWorldView. **/
	public Canvas3D getCanvas3D() {
		return canvas3d;
	}
	
	/** Returns the java3d node associated with this instance. This method
	 * should only be used by by the WorldViewFactory. **/
	public Node getNode() {
		return branchGroup;
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Returns true if the given property is set in the given properties. **/
	private static final boolean isSet(int properties, int property) {
		return ((properties & property) == property);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class ExtendedJCanvas3D extends JCanvas3D {
		
		// Auto generated serial version UID
		private static final long serialVersionUID = 3019030444897418643L;
		
		/*============================================================
		Constructors
		============================================================*/

		/** Creates an extended JCanvas3D with the provided dimension. **/
		public ExtendedJCanvas3D(Dimension d) {
			setSize(d);
			setPreferredSize(d);
		}
		
		/*============================================================
		Public Methods
		============================================================*/
	
		@Override public void paintComponent(Graphics g) {
			//background.repaint(getWorldEngine());
			super.paintComponent(g);
			hud.clear();
			//getWorldEngine().performPostDraw(hud);
			hud.paint(0, 0, g, this);
		} 
	
	}

	@Override
	public void preRender(WorldAppearance3D world) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postRender(WorldAppearance3D world) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean setSymbiosis(WorldViewSymbiosis symb) {
		// TODO Auto-generated method stub
		return false;
	}

}
